home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / plain / contrib / literate / makeprog.web < prev    next >
Encoding:
Text File  |  1989-11-22  |  35.0 KB  |  1,056 lines

  1. @                                                           % sect.    2
  2. The program begins with a fairly normal header, made up of
  3. pieces that will mostly be filled in later.  The input comes
  4. from files |doc_file| and |change_file|, the output goes to
  5. file |prog_file|.  Messages from \MAKEPROG{} are written to
  6. |term_out|, which is supposed to be the terminal.
  7. @^system dependencies@>
  8.  
  9. If it is necessary to abort the job because of a fatal
  10. error, the program calls the `|jump_out|' procedure, which
  11. goes to the label |end_of_MAKEPROG|.
  12.  
  13. @d end_of_MAKEPROG = 9999 {go here to wrap it up}
  14.  
  15. @p @t\4@>@<Compiler directives@>@/
  16. program MAKEPROG(@!doc_file,@!change_file,@!prog_file);
  17. label end_of_MAKEPROG; {go here to finish}
  18. const @<Constants in the outer block@>@;
  19. type @<Types in the outer block@>@;
  20. var @<Globals in the outer block@>@;
  21. @t\4@>@<Error handling procedures@>@;
  22. procedure initialize;
  23.   var @<Local variables for initialization@>@;
  24.   begin @<Set initial values@>
  25.   end;
  26.  
  27.  
  28. @                                                           % sect.    3
  29. Some of this code is optional for use when debugging only;
  30. such material is enclosed between the delimiters \&{debug}
  31. and \&{gubed}.
  32.  
  33. @d debug==@{ {change this to `$\\{debug}\equiv\null$' when debugging}
  34. @d gubed==@t@>@} {change this to `$\\{gubed}\equiv\null$' when debugging}
  35. @f debug==repeat
  36. @f gubed==until
  37.  
  38.  
  39. @                                                           % sect.    4
  40. The \PASCAL\ compiler used to develop this system has
  41. ``compiler directives'' that can appear in comments whose
  42. first character is a dollar sign.  In production versions of
  43. \MAKEPROG{} these directives tell the compiler that it is
  44. safe to avoid range checks and to leave out the extra code
  45. it inserts for the \PASCAL\ debugger's benefit.
  46. @^system dependencies@>
  47.  
  48. @<Compiler directives@>=
  49. @{@&@=$D-@> @} {no debug overhead}
  50. @!debug @{@&@=$D+@> @}@+ gubed @; {but turn everything on when debugging}
  51.  
  52.  
  53. @                                                           % sect.    5
  54. Labels are given symbolic names by the following
  55. definitions.  We insert the label `|exit|' just before the
  56. `\&{end}' of a procedure in which we have used the
  57. `|return|' statement defined below; the label `|restart|' is
  58. occasionally used at the very beginning of a procedure; and
  59. the label `|reswitch|' is occasionally used just prior to a
  60. \&{case} statement in which some cases change the conditions
  61. and we wish to branch to the newly applicable case.  Loops
  62. that are set up with the \&{loop} construction defined below
  63. are commonly exited by going to `|done|' or to `|found|' or
  64. to `|not_found|', and they are sometimes repeated by going
  65. to `|continue|'.
  66.  
  67. @d exit=10 {go here to leave a procedure}
  68. @d restart=20 {go here to start a procedure again}
  69. @d reswitch=21 {go here to start a case statement again}
  70. @d continue=22 {go here to resume a loop}
  71. @d done=30 {go here to exit a loop}
  72. @d found=31 {go here when you've found it}
  73. @d not_found=32 {go here when you've found something else}
  74.  
  75.  
  76. @                                                           % sect.    6
  77. Here are some macros for common programming idioms.
  78.  
  79. @d incr(#) == #:=#+1 {increase a variable by unity}
  80. @d decr(#) == #:=#-1 {decrease a variable by unity}
  81. @d loop == @+ while true do@+ {repeat over and over until a |goto| happens}
  82. @d do_nothing == {empty statement}
  83. @d return == goto exit {terminate a procedure call}
  84. @f return == nil
  85. @f loop == xclause
  86.  
  87.  
  88. @                                                           % sect.    7
  89. We assume that |case| statements may include a default case
  90. that applies if no matching label is found.  Thus, we shall
  91. use constructions like
  92. @^system dependencies@>
  93. $$
  94. \vbox{\halign{#\hfil\cr
  95. |case x of|\cr
  96.    \quad 1: $\langle\,$code for $x=1\,\rangle$;\cr
  97.    \quad 3: $\langle\,$code for $x=3\,\rangle$;\cr
  98.    \quad |othercases| $\langle\,$code for |x<>1| and |x<>3|$\,\rangle$\cr
  99. |endcases|\cr
  100. }}
  101. $$
  102. since most \PASCAL\ compilers have plugged this hole in the
  103. language by incorporating some sort of default mechanism.
  104. For example, the compiler used to develop \.{WEB} and \TeX\
  105. allows `|others|:' as a default label, and other \PASCAL s
  106. allow syntaxes like `\&{else}' or `\&{otherwise}' or
  107. `\\{otherwise}:', etc.  The definitions of |othercases| and
  108. |endcases| should be changed to agree with local
  109. conventions.  (Of course, if no default mechanism is
  110. available, the |case| statements of this program must be
  111. extended by listing all remaining cases.)
  112.  
  113. @d othercases == others: {default for cases not listed explicitly}
  114. @d endcases == @+end {follows the default case in an extended |case| statement}
  115. @f othercases == else
  116. @f endcases == end
  117.  
  118.  
  119. @                                                           % sect.    8
  120. The following parameter is set big enough to be sufficient
  121. for most applications of \MAKEPROG{}.
  122.  
  123. @<Constants...@>=
  124. @!buf_size=500; {maximum length of input line}
  125.  
  126.  
  127. @                                                           % sect.    9
  128. A global variable called |history| will contain one of four values
  129. at the end of every run: |spotless| means that no unusual messages were
  130. printed; |harmless_message| means that a message of possible interest
  131. was printed but no serious errors were detected; |error_message| means that
  132. at least one error was found; |fatal_message| means that the program
  133. terminated abnormally. The value of |history| does not influence the
  134. behavior of the program; it is simply computed for the convenience
  135. of systems that might want to use such information.
  136.  
  137. @d spotless=0 {|history| value for normal jobs}
  138. @d harmless_message=1 {|history| value when non-serious info was printed}
  139. @d error_message=2 {|history| value when an error was noted}
  140. @d fatal_message=3 {|history| value when we had to stop prematurely}
  141. @#
  142. @d mark_harmless==@t@>@+if history=spotless then history:=harmless_message
  143. @d mark_error==history:=error_message
  144. @d mark_fatal==history:=fatal_message
  145.  
  146. @<Glob...@>=@!history:spotless..fatal_message; {how bad was this run?}
  147.  
  148. @                                                           % sect.   10
  149. @<Set init...@>=history:=spotless;
  150.  
  151.  
  152.  
  153.  
  154.  
  155. @* The character set.                                       % sect.   11
  156.  
  157. \noindent One of the main goals in the design of \MAKEPROG{}
  158. has been to make it readily portable between a wide variety
  159. of computers.  Yet \MAKEPROG{} by its very nature must use a
  160. greater variety of characters than most computer programs
  161. deal with, and character encoding is one of the areas in
  162. which existing machines differ most widely from each other.
  163.  
  164. To resolve this problem, all input to \MAKEPROG{} is
  165. converted to an internal seven-bit code that is essentially
  166. standard \ASCII{}, the ``American Standard Code for
  167. Information Interchange.'' The conversion is done
  168. immediately when each character is read in.  Conversely,
  169. characters are converted from \ASCII{} to the user's
  170. external representation just before they are output.  Such
  171. an internal code is never relevant to users of \MAKEPROG{}.
  172.  
  173. \noindent Here is a table of the standard visible \ASCII{} codes:
  174. $$\def\:{\char\count255\global\advance\count255 by 1}
  175. \count255='40
  176. \vbox{
  177. \hbox{\hbox to 40pt{\it\hfill0\/\hfill}%
  178. \hbox to 40pt{\it\hfill1\/\hfill}%
  179. \hbox to 40pt{\it\hfill2\/\hfill}%
  180. \hbox to 40pt{\it\hfill3\/\hfill}%
  181. \hbox to 40pt{\it\hfill4\/\hfill}%
  182. \hbox to 40pt{\it\hfill5\/\hfill}%
  183. \hbox to 40pt{\it\hfill6\/\hfill}%
  184. \hbox to 40pt{\it\hfill7\/\hfill}}
  185. \vskip 4pt
  186. \hrule
  187. \def\^{\vrule height 10.5pt depth 4.5pt}
  188. \halign{\hbox to 0pt{\hskip -24pt\O{#0}\hfill}&\^
  189. \hbox to 40pt{\tt\hfill#\hfill\^}&
  190. &\hbox to 40pt{\tt\hfill#\hfill\^}\cr
  191. 04&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  192. 05&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  193. 06&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  194. 07&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  195. 10&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  196. 11&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  197. 12&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  198. 13&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  199. 14&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  200. 15&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  201. 16&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  202. 17&\:&\:&\:&\:&\:&\:&\:\cr}
  203. \hrule width 280pt}$$
  204. (Actually, of course, code @"20 is an invisible blank
  205. space.) Code @"3E was once an upward arrow (\.{\char'13}),
  206. and code @"3F was once a left arrow (\.^^X), in olden times
  207. when the first draft of \ASCII{} code was prepared; but
  208. \MAKEPROG{} works with today's standard \ASCII{} in which
  209. those codes represent circumflex and underline as shown.
  210.  
  211. @<Types...@>=
  212. @!ASCII_code=0..@"7F; {seven-bit numbers, a subrange of the integers}
  213.  
  214.  
  215. @                                                           % sect.   12
  216. The original \PASCAL\ compiler was designed in the late 60s,
  217. when six-bit character sets were common, so it did not make
  218. provision for lowercase letters.  Nowadays, of course, we
  219. need to deal with both capital and small letters in a
  220. convenient way, so \MAKEPROG{} assumes that it is being used
  221. with a \PASCAL\ whose character set contains at least the
  222. characters of standard \ASCII{} as listed above.  Some
  223. \PASCAL\ compilers use the original name |char| for the data
  224. type associated with the characters in text files, while
  225. other \PASCAL s consider |char| to be a 64-element subrange
  226. of a larger data type that has some other name.
  227.  
  228. In order to accommodate this difference, we shall use the
  229. name |text_char| to stand for the data type of the
  230. characters in the input and output files.  We shall also
  231. assume that |text_char| consists of the elements
  232. |chr(first_text_char)| through |chr(last_text_char)|,
  233. inclusive.  The following definitions should be adjusted if
  234. necessary.
  235. @^system dependencies@>
  236.  
  237. @d text_char == char {the data type of characters in text files}
  238. @d first_text_char=0 {ordinal number of the smallest element of |text_char|}
  239. @d last_text_char=127 {ordinal number of the largest element of |text_char|}
  240.  
  241. @<Types...@>=
  242. @!text_file=packed file of text_char;
  243.  
  244.  
  245. @                                                           % sect.   13
  246. The \MAKEPROG{} processor convert between \ASCII{} code and
  247. the user's external character set by means of arrays |xord|
  248. and |xchr| that are analogous to \PASCAL's |ord| and |chr|
  249. functions.
  250.  
  251. @<Globals...@>=
  252. @!xord: array [text_char] of ASCII_code;
  253.   {specifies conversion of input characters}
  254. @!xchr: array [ASCII_code] of text_char;
  255.   {specifies conversion of output characters}
  256.  
  257.  
  258. @                                                           % sect.   14
  259. If we assume that every system using \.{WEB} is able to read
  260. and write the visible characters of standard \ASCII{}
  261. (although not necessarily using the \ASCII{} codes to
  262. represent them), the following assignment statements
  263. initialize most of the |xchr| array properly, without
  264. needing any system-dependent changes.  For example, the
  265. statement \.{xchr["A"]:=\'A\'} that appears in the present
  266. \.{WEB} file might be encoded in, say, \hbox{\mc EBCDIC}
  267. code on the external medium on which it resides, but
  268. \.{TANGLE} will convert from this external code to \ASCII{}
  269. and back again.  Therefore the assignment statement
  270. \.{XCHR[65]:=\'A\'} will appear in the corresponding
  271. \PASCAL\ file, and \PASCAL\ will compile this statement so
  272. that |xchr[65]| receives the character \.A in the external
  273. (|char|) code.  Note that it would be quite incorrect to say
  274. \.{xchr["A"]:="A"}, because |"A"| is a constant of type
  275. |integer|, not |char|, and because we have $|"A"|=65$
  276. regardless of the external character set.
  277.  
  278. @<Set init...@>=
  279. xchr[" "]:=' ';
  280. xchr["!"]:='!';
  281. xchr[""""]:='"';
  282. xchr["#"]:='#';
  283. xchr["$"]:='$';
  284. xchr["%"]:='%';
  285. xchr["&"]:='&';
  286. xchr["'"]:='''';@/
  287. xchr["("]:='(';
  288. xchr[")"]:=')';
  289. xchr["*"]:='*';
  290. xchr["+"]:='+';
  291. xchr[","]:=',';
  292. xchr["-"]:='-';
  293. xchr["."]:='.';
  294. xchr["/"]:='/';@/
  295. xchr["0"]:='0';
  296. xchr["1"]:='1';
  297. xchr["2"]:='2';
  298. xchr["3"]:='3';
  299. xchr["4"]:='4';
  300. xchr["5"]:='5';
  301. xchr["6"]:='6';
  302. xchr["7"]:='7';@/
  303. xchr["8"]:='8';
  304. xchr["9"]:='9';
  305. xchr[":"]:=':';
  306. xchr[";"]:=';';
  307. xchr["<"]:='<';
  308. xchr["="]:='=';
  309. xchr[">"]:='>';
  310. xchr["?"]:='?';@/
  311. xchr["@@"]:='@@';
  312. xchr["A"]:='A';
  313. xchr["B"]:='B';
  314. xchr["C"]:='C';
  315. xchr["D"]:='D';
  316. xchr["E"]:='E';
  317. xchr["F"]:='F';
  318. xchr["G"]:='G';@/
  319. xchr["H"]:='H';
  320. xchr["I"]:='I';
  321. xchr["J"]:='J';
  322. xchr["K"]:='K';
  323. xchr["L"]:='L';
  324. xchr["M"]:='M';
  325. xchr["N"]:='N';
  326. xchr["O"]:='O';@/
  327. xchr["P"]:='P';
  328. xchr["Q"]:='Q';
  329. xchr["R"]:='R';
  330. xchr["S"]:='S';
  331. xchr["T"]:='T';
  332. xchr["U"]:='U';
  333. xchr["V"]:='V';
  334. xchr["W"]:='W';@/
  335. xchr["X"]:='X';
  336. xchr["Y"]:='Y';
  337. xchr["Z"]:='Z';
  338. xchr["["]:='[';
  339. xchr["\"]:='\';
  340. xchr["]"]:=']';
  341. xchr["^"]:='^';
  342. xchr["_"]:='_';@/
  343. xchr["`"]:='`';
  344. xchr["a"]:='a';
  345. xchr["b"]:='b';
  346. xchr["c"]:='c';
  347. xchr["d"]:='d';
  348. xchr["e"]:='e';
  349. xchr["f"]:='f';
  350. xchr["g"]:='g';@/
  351. xchr["h"]:='h';
  352. xchr["i"]:='i';
  353. xchr["j"]:='j';
  354. xchr["k"]:='k';
  355. xchr["l"]:='l';
  356. xchr["m"]:='m';
  357. xchr["n"]:='n';
  358. xchr["o"]:='o';@/
  359. xchr["p"]:='p';
  360. xchr["q"]:='q';
  361. xchr["r"]:='r';
  362. xchr["s"]:='s';
  363. xchr["t"]:='t';
  364. xchr["u"]:='u';
  365. xchr["v"]:='v';
  366. xchr["w"]:='w';@/
  367. xchr["x"]:='x';
  368. xchr["y"]:='y';
  369. xchr["z"]:='z';
  370. xchr["{"]:='{';
  371. xchr["|"]:='|';
  372. xchr["}"]:='}';
  373. xchr["~"]:='~';@/
  374. xchr[0]:=' '; xchr[@"7F]:=' '; {these \ASCII{} codes are not used}
  375.  
  376.  
  377. @                                                           % sect.   15
  378. Some of the nonprintable \ASCII{} codes have been given
  379. symbolic names in \MAKEPROG{} because they are used with a
  380. special meaning.
  381.  
  382. @d tab_mark=@"09              {\ASCII{} code used as tab-skip}
  383. @d line_feed=@"0A             {\ASCII{} code thrown away at end of line}
  384. @d form_feed=@"0C             {\ASCII{} code used at end of page}
  385. @d carriage_return=@"0D       {\ASCII{} code used at end of line}
  386.  
  387.  
  388. @                                                           % sect.   16
  389. When we initialize the |xord| array and the remaining parts of |xchr|,
  390. it will be convenient to make use of an index variable, |i|.
  391.  
  392. @<Local variables for init...@>=
  393. @!i:0..last_text_char;
  394.  
  395.  
  396. @                                                           % sect.   17
  397. Here now is the system-dependent part of the character set.
  398. If \MAKEPROG{} is being implemented on a garden-variety
  399. \PASCAL\ for which only standard \ASCII{} codes will appear
  400. in the input and output files, you don't need to make any
  401. changes here.
  402. @^system dependencies@>
  403.  
  404. Changes to the present module will make \MAKEPROG{} more
  405. friendly on computers that have an extended character set,
  406. so that one can type things like Umlaute.  If you have an
  407. extended set of characters that are easily incorporated into
  408. text files, you can assign codes arbitrarily here, giving an
  409. |xchr| equivalent to whatever characters the users of
  410. \MAKEPROG{} are allowed to have in their input files,
  411. provided that unsuitable characters do not correspond to
  412. special codes like |carriage_return| that are listed above.
  413.  
  414. @<Set init...@>=
  415. for i:=1 to " "-1 do xchr[i]:=' ';
  416.  
  417.  
  418. @                                                           % sect.   18
  419. The following system-independent code makes the |xord| array
  420. contain a suitable inverse to the information in |xchr|.
  421.  
  422. @<Set init...@>=
  423. for i:=first_text_char to last_text_char do xord[chr(i)]:=" ";
  424. for i:=1 to "~" do xord[xchr[i]]:=i;
  425.  
  426.  
  427.  
  428.  
  429.  
  430. @* Basic Input and output.
  431.  
  432. \noindent The input conventions of \MAKEPROG{} are identical
  433. to those of \.{WEB}.  Therefore people who need to make
  434. modifications to both systems should be able to do so
  435. without too many headaches.
  436.  
  437.  
  438. @                                                           % sect.   20
  439. Terminal output is done by writing on file |term_out|, which
  440. is assumed to consist of characters of type |text_char|:
  441. @^system dependencies@>
  442.  
  443. @d print(#)==write(term_out,#) {`|print|' means write on the terminal}
  444. @d print_ln(#)==write_ln(term_out,#) {`|print|' and then start new line}
  445. @d new_line==write_ln(term_out) {start new line}
  446. @d print_nl(#)==  {print information starting on a new line}
  447.   begin new_line; print(#);
  448.   end
  449.  
  450. @<Globals...@>=
  451. @!term_out:text_file; {the terminal as an output file}
  452.  
  453.  
  454. @                                                           % sect.   21
  455. Different systems have different ways of specifying that the
  456. output on a certain file will appear on the user's terminal.
  457. Here is one way to do this on the \PASCAL{} system that was
  458. used in \.{TANGLE}'s initial development.
  459. @^system dependencies@>
  460.  
  461. @<Set init...@>=
  462. rewrite(term_out,'TTY:'); {send |term_out| output to the terminal}
  463.  
  464.  
  465. @                                                           % sect.   22
  466. The |update_terminal| procedure is called when we want to
  467. make sure that everything we have output to the terminal so
  468. far has actually left the computer's internal buffers and
  469. been sent.
  470. @^system dependencies@>
  471.  
  472. @d update_terminal == break(term_out) {empty the terminal output buffer}
  473.  
  474.  
  475. @                                                           % sect.   23
  476. The main input comes from |doc_file|; this input may be
  477. overridden by changes in |change_file|.  (If |change_file|
  478. is empty, there are no changes.)
  479.  
  480. @<Globals...@>=
  481. @!doc_file:text_file; {primary input}
  482. @!change_file:text_file; {updates}
  483.  
  484.  
  485. @                                                           % sect.   24
  486. The following code opens the input files.  Since these files
  487. were listed in the program header, we assume that the
  488. \PASCAL\ runtime system has already checked that suitable
  489. file names have been given; therefore no additional error
  490. checking needs to be done.
  491. @^system dependencies@>
  492.  
  493. @< Set init... @>=
  494. reset(doc_file); reset(change_file);
  495.  
  496.  
  497. @                                                           % sect.   25
  498. The output goes to |prog_file|.
  499.  
  500. @<Globals...@>=
  501. @!prog_file: text_file;
  502.  
  503.  
  504. @                                                           % sect.   26
  505. The following code opens |prog_file|.  Since this file is
  506. listed in the program header, we assume that the \PASCAL\
  507. runtime system has checked that a suitable external file
  508. name have been given.
  509. @^system dependencies@>
  510.  
  511. @<Set init...@>=
  512. rewrite(prog_file);
  513.  
  514.  
  515. @                                                           % sect.   27
  516. Input goes into an array called |buffer|.
  517.  
  518. @<Globals...@>=
  519. @!buffer: array[0..buf_size] of ASCII_code;
  520.  
  521.  
  522. @                                                           % sect.   28
  523. The |input_ln| procedure brings the next line of input from
  524. the specified file into the |buffer| array and returns the
  525. value |true|, unless the file has already been entirely
  526. read, in which case it returns |false|.  The conventions of
  527. \.{WEB} are followed; i.e., |ASCII_code| numbers
  528. representing the next line of the file are input into
  529. |buffer[0]|, |buffer[1]|, \dots, |buffer[limit-1]|; trailing
  530. blanks are ignored; and the global variable |limit| is set
  531. to the length of the line.  The value of |limit| must be
  532. strictly less than |buf_size|.
  533. @^system dependencies@>
  534.  
  535. We assume that none of the |ASCII_code| values of
  536. |buffer[j]| for |0<=j<limit| is equal to 0, @"7F,
  537. |line_feed|, |form_feed|, or |carriage_return|.
  538.  
  539. @p
  540. function input_ln(var f:text_file):boolean; {inputs a line or returns |false|}
  541.    var final_limit:0..buf_size; {|limit| without trailing blanks}
  542.    begin limit:=0; final_limit:=0;
  543.    if eof(f) then input_ln:=false
  544.    else  begin
  545.       while not eoln(f) do
  546.          begin buffer[limit]:=xord[f^]; get(f);
  547.          incr(limit);
  548.          if (buffer[limit-1]<>" ") and (buffer[limit-1]<>tab_mark) then
  549.             final_limit:=limit;
  550.          if limit=buf_size then
  551.             begin while not eoln(f) do get(f);
  552.             decr(limit); {keep |buffer[buf_size]| empty}
  553.             print_nl('! Input line too long'); error; mark_error;
  554. @.Input line too long@>
  555.             end;
  556.          end;
  557.       read_ln(f); limit:=final_limit; input_ln:=true;
  558.       end;
  559.    end;
  560.  
  561.  
  562.  
  563.  
  564.  
  565. @* Reporting errors to the user.                            % sect.   29
  566.  
  567. \noindent Errors are reported to the user by saying
  568. $$
  569.    \hbox{`|err_print('! Error message')|'},
  570. $$
  571. followed by `|jump_out|' if no recovery from the error is
  572. provided.  This will print the error message followed by an
  573. indication of where the error was spotted in the source
  574. file.  Note that no period follows the error message, since
  575. the error routine will automatically supply a period.
  576.  
  577. \noindent The actual error indications are provided by a
  578. procedure called |error|.
  579.  
  580. @d err_print(#)==begin print_nl(#); error; mark_error; end
  581.  
  582. @<Error handling...@>=
  583. procedure error; {prints '\..' and location of error message}
  584.    begin @< Print error location @>;
  585.    update_terminal;
  586.    end;
  587.  
  588.  
  589. @                                                           % sect.   32
  590. The error locations can be indicated by using the global
  591. variables |line| and |changing|, which tell respectively the
  592. the current line number and whether or not the current line
  593. is from |change_file| or |doc_file|.  This routine should be
  594. modified on systems whose standard text editor has special
  595. line-numbering conventions.
  596. @^system dependencies@>
  597.  
  598. @< Print error location @>=
  599. begin
  600. if changing then  print('. (change file ') @+ else print('. (');
  601. print_ln('l.', line:1, ')');
  602. print(' '); {this space separates the message from future output}
  603. end
  604.  
  605.  
  606. @                                                           % sect.   34
  607. The |jump_out| procedure just cuts across all active
  608. procedure levels and jumps out of the program.  This is the
  609. only non-local |goto| statement in \MAKEPROG{}.  It is used
  610. when no recovery from a particular error has been provided.
  611.  
  612. Some \PASCAL\ compilers do not implement non-local |goto| statements.
  613. @^system dependencies@>
  614. In such cases the code that appears at label
  615. |end_of_MAKEPROG| should be copied into the |jump_out|
  616. procedure, followed by a call to a system procedure that
  617. terminates the program.
  618.  
  619. @d fatal_error(#)==begin print_nl(#); error; mark_fatal; jump_out;
  620.   end
  621.  
  622. @<Error handling...@>=
  623. procedure jump_out;
  624. begin goto end_of_MAKEPROG;
  625. end;
  626.  
  627.  
  628. @                                                           % sect.   35
  629. Sometimes the program's behavior is far different from what
  630. it should be, and \MAKEPROG{} prints an error message that
  631. is really for the \MAKEPROG{} maintenance person, not the
  632. user.  In such cases the program says
  633. |confusion('indication of where we are')|.
  634.  
  635. @d confusion(#)==fatal_error('! This can''t happen (',#,')')
  636. @.This can't happen@>
  637.  
  638.  
  639.  
  640.  
  641.  
  642. @* The kernel.
  643.  
  644. \noindent Let us now consider the routine |get_line| that
  645. takes care of merging |change_file| into |doc_file|.  The
  646. |get_line| procedure also updates the line numbers for error
  647. messages.
  648.  
  649. @<Globals...@>=
  650. @!line:integer; {the number of the current line in the current file}
  651. @!other_line:integer; {the number of the current line in the input file that
  652.   is not currently being read}
  653. @!temp_line:integer; {used when interchanging |line| with |other_line|}
  654. @!limit:0..buf_size; {the last character position occupied in the buffer}
  655. @!input_has_ended: boolean; {if |true|, there is no more input}
  656. @!changing: boolean; {if |true|, the current line is from |change_file|}
  657.  
  658.  
  659. @                                                           % sect.  128
  660. As we change |changing| from |true| to |false| and back
  661. again, we must remember to swap the values of |line| and
  662. |other_line| so that the |err_print| routine will be sure to
  663. report the correct line number.
  664.  
  665. @d change_changing==
  666.          begin changing := not changing;@/
  667.          temp_line:=other_line; other_line:=line; line:=temp_line;
  668.          end {$|line| \BA |other_line|$}
  669.  
  670.  
  671. @                                                           % sect.  129
  672. When |changing| is |false|, the next line of |change_file|
  673. is kept in |change_buffer[0..change_limit-1]|, for purposes
  674. of comparison with the next line of |doc_file|.  After the
  675. change file has been completely input, we set
  676. |change_limit:=0|, so that no further matches will be made.
  677.  
  678. @<Globals...@>=
  679. @!change_buffer:array[0..buf_size] of ASCII_code;
  680. @!change_limit:0..buf_size; {the last position occupied in |change_buffer|}
  681.  
  682.  
  683. @                                                           % sect.  130
  684. Here's a simple function that checks if the two buffers are
  685. different.
  686.  
  687. @p function lines_dont_match:boolean;
  688. label exit;
  689. var k:0..buf_size; {index into the buffers}
  690. begin lines_dont_match:=true;
  691. if change_limit<>limit then return;
  692. if limit>0 then
  693.   for k:=0 to limit-1 do if change_buffer[k]<>buffer[k] then return;
  694. lines_dont_match:=false;
  695. exit: end;
  696.  
  697.  
  698. @                                                           % sect.  131
  699. Procedure |prime_the_change_buffer| sets |change_buffer| in
  700. preparation for the next matching operation.  Since blank
  701. lines in the change file are not used for matching, we have
  702. |(change_limit=0)and not changing| if and only if the change
  703. file is exhausted.  This procedure is called only when
  704. |changing| is true; hence error messages will be reported
  705. correctly.
  706.  
  707. @p procedure prime_the_change_buffer;
  708. label continue, done, exit;
  709. var k:0..buf_size; {index into the buffers}
  710. begin change_limit:=0; {this value will be used if the change file ends}
  711. @<Skip over comment lines in the change file; |return| if end of file@>;
  712. @<Skip to the next nonblank line; |return| if end of file@>;
  713. @<Move |buffer| and |limit| to |change_buffer| and |change_limit|@>;
  714. exit: end;
  715.  
  716.  
  717. @                                                           % sect.  132
  718. While looking for a line that begins with \.{@@x} in the
  719. change file, we allow lines that begin with \.{@@}, as long
  720. as they don't begin with \.{@@y} or \.{@@z} (which would
  721. probably indicate that the change file is fouled up).
  722.  
  723. @<Skip over comment lines in the change file...@>=
  724. loop@+  begin incr(line);
  725.   if not input_ln(change_file) then return;
  726.   if limit<2 then goto continue;
  727.   if buffer[0]<>"@@" then goto continue;
  728.   if (buffer[1]>="X")and(buffer[1]<="Z") then
  729.     buffer[1]:=buffer[1]+"z"-"Z"; {lowercasify}
  730.   if buffer[1]="x" then goto done;
  731.   if (buffer[1]="y")or(buffer[1]="z") then
  732.     err_print('! Where is the matching @@x?');
  733. @.Where is the match...@>
  734. continue: end;
  735. done:
  736.  
  737.  
  738. @                                                           % sect.  133
  739. Here we are looking at lines following the \.{@@x}.
  740.  
  741. @<Skip to the next nonblank line...@>=
  742. repeat incr(line);
  743.   if not input_ln(change_file) then
  744.     begin err_print('! Change file ended after @@x');
  745. @.Change file ended...@>
  746.     return;
  747.     end;
  748. until limit>0;
  749.  
  750.  
  751. @                                                           % sect.  134
  752. @<Move |buffer| and |limit| to |change_buffer| and |change_limit|@>=
  753. begin change_limit:=limit;
  754. for k:=0 to limit-1 do change_buffer[k]:=buffer[k];
  755. end
  756.  
  757.  
  758. @                                                           % sect.  135
  759. The following procedure is used to see if the next change
  760. entry should go into effect; it is called only when
  761. |changing| is false.  The idea is to test whether or not the
  762. current contents of |buffer| matches the current contents of
  763. |change_buffer|.  If not, there's nothing more to do; but if
  764. so, a change is called for:  All of the text down to the
  765. \.{@@y} is supposed to match.  An error message is issued if
  766. any discrepancy is found.  Then the procedure prepares to
  767. read the next line from |change_file|.
  768.  
  769. @p
  770. procedure check_change; {switches to |change_file| if the buffers match}
  771. label exit;
  772. var n:integer; {the number of discrepancies found}
  773. @!k:0..buf_size; {index into the buffers}
  774. begin if lines_dont_match then return;
  775. n:=0;
  776. loop@+  begin change_changing; {now it's |true|}
  777.   incr(line);
  778.   if not input_ln(change_file) then
  779.     begin err_print('! Change file ended before @@y');@/
  780. @.Change file ended...@>
  781.     change_limit:=0;  change_changing; {|false| again}
  782.     return;
  783.     end;
  784.   @<If the current line starts with \.{@@y},
  785.     report any discrepancies and |return|@>;
  786.   @<Move |buffer| and |limit|...@>;
  787.   change_changing; {now it's |false|}
  788.   incr(line);
  789.   if not input_ln(doc_file) then
  790.     begin err_print('! CWEB file ended during a change');
  791. @.CWEB file ended...@>
  792.     input_has_ended:=true; return;
  793.     end;
  794.   if lines_dont_match then incr(n);
  795.   end;
  796. exit: end;
  797.  
  798.  
  799. @                                                           % sect.  136
  800. @<If the current line starts with \.{@@y}...@>=
  801. if limit>1 then if buffer[0]="@@" then
  802.   begin if (buffer[1]>="X")and(buffer[1]<="Z") then
  803.     buffer[1]:=buffer[1]+"z"-"Z"; {lowercasify}
  804.   if (buffer[1]="x")or(buffer[1]="z") then
  805.     err_print('! Where is the matching @@y?')
  806. @.Where is the match...@>
  807.   else if buffer[1]="y" then
  808.     begin if n>0 then
  809.       err_print('! Hmm... ',n:1,' of the preceding lines failed to match');
  810. @.Hmm... n of the preceding...@>
  811.     return;
  812.     end;
  813.   end
  814.  
  815.  
  816. @                                                           % sect.  137
  817. @< Initialize the input system @>=
  818. begin line:=0; other_line:=0;@/
  819. changing:=true; prime_the_change_buffer; change_changing;@/
  820. limit:=0; buffer[0]:=" "; input_has_ended:=false;
  821. end
  822.  
  823.  
  824. @                                                           % sect.  138
  825. The |get_line| procedure puts the next line of merged input
  826. into the buffer and updates the other variables
  827. appropriately.  A space is placed at the right end of the
  828. line.  We output points to show the user the progress in
  829. reading.
  830.  
  831. @p
  832. procedure get_line; {inputs the next line}
  833.    label restart;
  834.    begin
  835. restart: if changing then
  836.       @<Read from |change_file| and maybe turn off |changing|@>;
  837.    if not changing then
  838.       begin @<Read from |doc_file| and maybe turn on |changing|@>;
  839.       if changing then  goto restart;
  840.       end;
  841.    buffer[limit]:=" ";
  842.    if (line mod 500) = 0 then
  843.       begin print(line:1);  update_terminal;
  844.       end
  845.    else if (line mod 100) = 0 then
  846.          begin print('.');  update_terminal;
  847.          end
  848.       @!debug else begin print('.');  update_terminal; @+ end @+ gubed @;
  849.       ;@/
  850.    end;
  851.  
  852.  
  853. @                                                           % sect.  139
  854. @<Read from |doc_file|...@>=
  855. begin incr(line);
  856. if not input_ln(doc_file) then input_has_ended:=true
  857. else if change_limit>0 then check_change;
  858. end
  859.  
  860.  
  861. @                                                           % sect.  140
  862. @<Read from |change_file|...@>=
  863. begin incr(line);
  864. if not input_ln(change_file) then
  865.   begin err_print('! Change file ended without @@z');
  866. @.Change file ended...@>
  867.   buffer[0]:="@@"; buffer[1]:="z"; limit:=2;
  868.   end;
  869. if limit>1 then {check if the change has ended}
  870.   if buffer[0]="@@" then
  871.     begin if (buffer[1]>="X")and(buffer[1]<="Z") then
  872.       buffer[1]:=buffer[1]+"z"-"Z"; {lowercasify}
  873.     if (buffer[1]="x")or(buffer[1]="y") then
  874.       err_print('! Where is the matching @@z?')
  875. @.Where is the match...@>
  876.     else if buffer[1]="z" then
  877.       begin prime_the_change_buffer; change_changing;
  878.       end;
  879.     end;
  880. end
  881.  
  882.  
  883. @                                                           % sect.  141
  884. At the end of the program, we will tell the user if the
  885. change file had a line that didn't match any relevant line
  886. in |doc_file|.
  887.  
  888. @<Check that all changes have been read@>=
  889. begin
  890. if change_limit<>0 then {|changing| is false}
  891.    begin
  892.    for limit:=0 to change_limit do  buffer[limit] := change_buffer[limit];
  893.    limit := change_limit;  changing := true;  line := other_line;
  894.    err_print('! Change file entry did not match');
  895. @.Change file entry did not match@>
  896.    end;
  897. end
  898.  
  899.  
  900. @
  901. The |put_line| procedure outputs the next line from
  902. |buffer| to |prog_file|. Perhaps we should give here a
  903. progress report, too (with asterisks?)
  904.  
  905. @p
  906. procedure put_line;
  907.    var i: 0..buf_size;
  908.    begin
  909.    for i:=0 to limit-1 do  write(prog_file, xchr[buffer[i]]);
  910.    write_ln(prog_file);
  911.    end;
  912.  
  913.  
  914.  
  915.  
  916.  
  917. @* The main program.                                        % sect.  190
  918.  
  919. \noindent We have defined some procedures, and it is time to
  920. use them---here is where \MAKEPROG{} starts, and where it
  921. ends.
  922. @^system dependencies@>
  923.  
  924. @p
  925.    begin initialize;
  926.    print_ln(banner); {print a ``banner line''}
  927.    print_ln(copy_right); print_ln(rights_res); {print a copyright notice}
  928.    @< Initialize the input system @>;
  929.    debug  print_ln('begin copy');
  930.    gubed @;
  931.    @< Copy all program parts to the output @>;
  932.    debug  print_ln('end copy');
  933.    gubed @;
  934.    @< Check that all changes... @>;
  935. end_of_MAKEPROG:
  936.    @#
  937.    {here files should be closed if the operating system requires it}
  938.    @;@#
  939.    @<Print the job |history|@>;
  940.    end.
  941.  
  942.  
  943. @
  944. A program part begins after a line that begins with
  945. \.{\\beginprog} and ends before the next line with
  946. \.{\\endprog} starting it. If we find the starting line we
  947. set |state| to |begin_prog|, between to lines |state| has
  948. the value |inner_prog| and with the ending line |state| is
  949. set to |out_of_prog|.
  950.  
  951. @d begin_prog = 0
  952. @d inner_prog = 1
  953. @d out_of_prog = 2
  954.  
  955. @< Glob... @>=
  956. @!state: begin_prog..out_of_prog;
  957.  
  958. @
  959. @< Set init... @>=
  960. state := out_of_prog;
  961.  
  962.  
  963. @
  964. After we have read the introducing line for a program part
  965. which is signaled with |state=begin_prog| we change the
  966. state to |inner_prog| to start copy the next line;
  967.  
  968. @< Copy all program... @>=
  969. begin get_line;
  970. while not input_has_ended do
  971.    begin @< Look at the input line and store in |state| the result @>;
  972.    if state = inner_prog then  put_line
  973.    else if state = begin_prog then  state := inner_prog;
  974.    get_line;
  975.    end;
  976. if state = inner_prog then  err_print('! Input has ended prematurely');
  977. @.Input has ended...@>
  978. end
  979.  
  980.  
  981. @
  982. We first define a few macros to improve the readability of
  983. the program part behind. After \.{\\beginprog} no letters
  984. may appear and after \.{\\endprog} either the line is ended
  985. or white space is to be there. The comparison is facilitated
  986. by the fact that |buffer[limit]=" "|, i.e.\ the last
  987. character of a line is always a space.
  988.  
  989. @d cmp_prog(#) == (buffer[#]="p") and (buffer[#+1]="r") and
  990.                   (buffer[#+2]="o") and (buffer[#+3]="g")
  991. @d cmp_begin == (buffer[1]="b") and (buffer[2]="e") and (buffer[3]="g") and@|
  992.                 (buffer[4]="i") and (buffer[5]="n") and cmp_prog(6) and@|
  993.                 ((buffer[10]<"A") or (buffer[10]>"Z")) and@|
  994.                 ((buffer[10]<"a") or (buffer[10]>"z"))
  995. @d cmp_end == (buffer[1]="e") and (buffer[2]="n") and (buffer[3]="d") and
  996.               cmp_prog(4) and @|
  997.               ((buffer[8]=" ") or (buffer[8]=tab_mark))
  998.  
  999. @< Look at the input... @>=
  1000. begin
  1001. if buffer[0] = "\" then
  1002.    if limit >= 10 then
  1003.       begin @+ if cmp_begin then  state := begin_prog;  @+ end
  1004.    else if limit >= 8 then
  1005.       if cmp_end then  state := out_of_prog;
  1006. end
  1007.  
  1008.  
  1009. @                                                           % sect.  195
  1010. Some implementations may wish to pass the |history| value to the
  1011. operating system so that it can be used to govern whether or not other
  1012. programs are started. Here we simply report the history to the user.
  1013. @^system dependencies@>
  1014.  
  1015. @<Print the job |history|@>=
  1016. case history of
  1017. spotless: print_nl('(No errors were found.)');
  1018. harmless_message: print_nl('(Did you see the warning message above?)');
  1019. error_message: print_nl('(Pardon me, but I think I spotted something wrong.)');
  1020. fatal_message: print_nl('(That was a fatal error, my friend.)');
  1021. end {there are no other cases}
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027. @* System-dependent changes.                                % sect.  196
  1028.  
  1029. \noindent This module should be replaced, if necessary, by
  1030. changes to the program that are necessary to make
  1031. \MAKEPROG{} work at a particular installation.  It is
  1032. usually best to design your change file so that all changes
  1033. to previous modules preserve the module numbering; then
  1034. everybody's version will be consistent with the printed
  1035. program.  More extensive changes, which introduce new
  1036. modules, can be inserted here; then only the index itself
  1037. will get a new module number.
  1038. @^system dependencies@>
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044. @* Index.                                                   % sect.  197
  1045.  
  1046. \noindent Here is a cross-reference table for the
  1047. \MAKEPROG{} processor.  All modules in which an identifier
  1048. is used are listed with that identifier, except that
  1049. reserved words are indexed only when they appear in format
  1050. definitions, and the appearances of identifiers in module
  1051. names are not indexed.  Underlined entries correspond to
  1052. where the identifier was declared.  Error messages and a few
  1053. other things like ``system dependencies'' are indexed here
  1054. too.
  1055.  
  1056.